package work20241225;

/**
 * @author zdl
 * @date 2024/12/25
 */
// 死锁
    class DeadLock implements Runnable{
        private boolean flag;
        private Object obj1;
        private Object obj2;

    public DeadLock(boolean flag, Object obj1, Object obj2) {
        this.flag = flag;
        this.obj1 = obj1;
        this.obj2 = obj2;
    }

    @Override
    public void run() {
        if (flag){
            synchronized (obj1){
                System.out.println(Thread.currentThread().getName()+"获得资源!");
                synchronized (obj2){
                    System.out.println(Thread.currentThread().getName()+"获得资源!");
                }
            }
        }
        if (!flag){
            synchronized (obj2){
                System.out.println(Thread.currentThread().getName()+"获得资源!");
                synchronized (obj1){
                    System.out.println(Thread.currentThread().getName()+"获得资源!");
                }
            }
        }
    }
}
public class Demo3 {
    public static void main(String[] args) {
        DeadLock deadLock1 = new DeadLock(true, "哈哈哈", "嘿嘿嘿");
        DeadLock deadLock2 = new DeadLock(false, "哈哈哈", "嘿嘿嘿");
        new Thread(deadLock1,"线程1:").start();
        new Thread(deadLock2,"线程2:").start();
    }
}
